//
//  NoteListViewControl.m
//  NoteDemo
//
//  Created by 郑火 on 16/2/17.
//  Copyright © 2016年 zwz. All rights reserved.
//

#import "NoteListViewControl.h"


@implementation NoteListViewControl

- (void)awakeFromNib {
    [super awakeFromNib];
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
        self.clearsSelectionOnViewWillAppear = NO;
        self.preferredContentSize = CGSizeMake(320.0, 600.0);
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.tableView.dataSource = self;
    self.navigationItem.leftBarButtonItem = self.editButtonItem;
    
    self.detailViewControl = (DetailViewControl *)[[self.splitViewController.viewControllers lastObject] topViewController];
    
    //self.nbl = [[NoteBuinessLogic alloc]init];
    self.nbl = [NoteBuinessLogic new];
    self.nbl.delegate = self;
    [self.nbl findAllNotes];
    
    //初始化UIRefreshControl
    UIRefreshControl *rc = [[UIRefreshControl alloc] init];
    rc.attributedTitle = [[NSAttributedString alloc]initWithString:@"下拉刷新"];
    [rc addTarget:self action:@selector(refreshTableView) forControlEvents:UIControlEventValueChanged];
    self.refreshControl = rc;
}


-(void) refreshTableView
{
    if (self.refreshControl.refreshing) {
        self.refreshControl.attributedTitle = [[NSAttributedString alloc]initWithString:@"加载中..."];
        [self.nbl findAllNotes];
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Segues

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    NSLog(@"%@",segue);
    if ([[segue identifier] isEqualToString:@"showDetail"]) {
        
        DetailViewControl *controller = (DetailViewControl *)[[segue destinationViewController] topViewController];
        
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        Note  *note = self.objects[indexPath.row];
        controller.detailItem = note;
        
    }
}

#pragma mark - Table View

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.objects.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    
    Note  *note = self.objects[indexPath.row];
    cell.textLabel.text = note.content;
    cell.detailTextLabel.text = note.date;
    
    return cell;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        self.deletedIndex = indexPath.row;
        self.deletedNote = [self.objects objectAtIndex:indexPath.row];
        
        [self.nbl deleteNote: self.deletedNote];
        
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        
    }
}



#pragma mark - 处理通知
//查询所有数据方法 成功
- (void)findAllNotesFinished:(NSMutableArray *)list
{
    NSLog(@"Successs%d",list.count);
    
    for(Note *aa in list)
    {
        NSLog(@"aa = %@", aa.content);
    }
    self.objects  = list;
    
    [self.tableView reloadData];
    if (self.refreshControl) {
        [self.refreshControl endRefreshing];
        self.refreshControl.attributedTitle = [[NSAttributedString alloc]initWithString:@"下拉刷新"];
    }
}

//查询所有数据方法 失败
- (void)findAllNotesFailed:(NSError*)error
{
    
    NSString *errorStr = [error localizedDescription];
//    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"操作信息"
//                                                        message:errorStr
//                                                       delegate:nil
//                                              cancelButtonTitle:@"OK"
//                                              otherButtonTitles: nil];
//    [alertView show];
    UIAlertController *uac = [UIAlertController alertControllerWithTitle:@"操作信息" message:errorStr preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:nil];
    [uac addAction:okAction];
    [self presentViewController:uac animated:YES completion:nil];
    if (self.refreshControl) {
        [self.refreshControl endRefreshing];
        self.refreshControl.attributedTitle = [[NSAttributedString alloc]initWithString:@"下拉刷新"];
    }
}


//删除Note方法 成功
- (void)deleteNoteFinished
{
    
//    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"操作信息"
//                                                        message:@"删除成功。"
//                                                       delegate:nil
//                                              cancelButtonTitle:@"OK"
//                                              otherButtonTitles: nil];
//    [alertView show];
    
    UIAlertController *uac = [UIAlertController alertControllerWithTitle:@"操作信息" message:@"删除成功"preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        [self dismissViewControllerAnimated:YES completion:nil];
    }];
    [uac addAction:okAction];
    [self presentViewController:uac animated:YES completion:nil];

    [self.objects removeObjectAtIndex: self.deletedIndex];
    [self.tableView reloadData];
    
}

//删除Note方法 失败
- (void)deleteNoteFailed:(NSError *)error
{
    NSString *errorStr = [error localizedDescription];
//    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"操作信息"
//                                                        message:errorStr
//                                                       delegate:nil
//                                              cancelButtonTitle:@"OK"
//                                              otherButtonTitles: nil];
//    [alertView show];
    
    
    UIAlertController *uac = [UIAlertController alertControllerWithTitle:@"操作信息" message:errorStr preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        [self dismissViewControllerAnimated:YES completion:nil];
    }];
    [uac addAction:okAction];
    [self presentViewController:uac animated:YES completion:nil];

}
- (void)viewDidAppear:(BOOL)animated
{
    [self.nbl findAllNotes];
    //[self.refreshControl beginRefreshing];
    //[self refreshTableView];
}

@end
